#include "StdAfx.h"
#include "TextPanelUI.h"

CTextPanelUI::CTextPanelUI(void) : m_nLinks(0), 
m_uButtonState(0),//m_bIsPrivate(false),
m_TextColor(UICOLOR_EDIT_TEXT_NORMAL), 
m_BackColor(UICOLOR__INVALID)
{
	m_uTextStyle = DT_WORDBREAK;
	::ZeroMemory(m_rcLinks, sizeof(m_rcLinks));
}

CTextPanelUI::~CTextPanelUI(void)
{
}

LPCTSTR CTextPanelUI::GetClass() const
{
	return _T("TextPanelUI");
}

bool CTextPanelUI::Activate()
{
	if( !CLabelPanelUI::Activate() ) return false;
	if( m_nLinks > 0 ) m_pManager->SendNotify(this, _T("link"));
	return true;
}

void CTextPanelUI::SetTextColor(UITYPE_COLOR TextColor)
{
	m_TextColor = TextColor;
	Invalidate();
}

void CTextPanelUI::SetBkColor(UITYPE_COLOR BackColor)
{
	m_BackColor = BackColor;
	Invalidate();
}

UINT CTextPanelUI::GetControlFlags() const
{
	return m_nLinks > 0 ? UIFLAG_SETCURSOR : 0;
}

void CTextPanelUI::Event(TEventUI& event)
{
	if( event.Type == UIEVENT_SETCURSOR ) {
		for( int i = 0; i < m_nLinks; i++ ) {
			if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) {
				::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
				return;
			}
		}      
	}
	if( event.Type == UIEVENT_BUTTONDOWN && IsEnabled() ) {
		for( int i = 0; i < m_nLinks; i++ ) {
			if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) {
				m_uButtonState |= UISTATE_PUSHED;
				Invalidate();
				return;
			}
		}      
	}
	if( event.Type == UIEVENT_BUTTONUP ) {
		for( int i = 0; i < m_nLinks; i++ ) {
			if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) Activate();
		}      
		m_uButtonState &= ~UISTATE_PUSHED;
		Invalidate();
		return;
	}
	// When you move over a link
	if( m_nLinks > 0 && event.Type == UIEVENT_MOUSEMOVE ) Invalidate();
	if( m_nLinks > 0 && event.Type == UIEVENT_MOUSELEAVE ) Invalidate();
	CLabelPanelUI::Event(event);
}

void CTextPanelUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if( _tcscmp(pstrName, _T("textColor")) == 0 ) SetTextColor((UITYPE_COLOR)_ttoi(pstrValue));
	else if( _tcscmp(pstrName, _T("backColor")) == 0 ) SetBkColor((UITYPE_COLOR)_ttoi(pstrValue));
	/*else if( _tcscmp(pstrName, _T("position")) == 0 )
	{
		RECT rcPos = {0};
		LPTSTR pstr = NULL;
		rcPos.left = _tcstol(pstrValue,&pstr,10);
		ASSERT(pstr);
		rcPos.top = _tcstol(pstr + 1,&pstr,10);
		ASSERT(pstr);
		rcPos.right = _tcstol(pstr + 1,&pstr,10);
		ASSERT(pstr);
		rcPos.bottom = _tcstol(pstr + 1,&pstr,10);
		ASSERT(pstr);
		SetRect(rcPos);
		m_bIsPrivate = true;
	}*/
	else CLabelPanelUI::SetAttribute(pstrName, pstrValue);
}

SIZE CTextPanelUI::EstimateSize(SIZE szAvailable)
{
	RECT rcText = { 0, 0, MAX(szAvailable.cx, m_cxWidth), 9999 };
	m_nLinks = 0;
	CBlueRenderEngineUI::DoPaintPrettyText(m_pManager->GetPaintDC(), m_pManager, rcText, m_sText, 
		UICOLOR_EDIT_TEXT_NORMAL, UICOLOR__INVALID, NULL, m_nLinks, DT_CALCRECT | m_uTextStyle);
	//return CStdSize(rcText.right - rcText.left, rcText.bottom - rcText.left);
	//if(m_bIsPrivate)
		//return CStdSize(rcText.right - rcText.left, rcText.bottom - rcText.top + 10);
	return CStdSize(rcText.right - rcText.left, rcText.bottom - rcText.top);
}

void CTextPanelUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	RECT rcText;
	/*if(m_bIsPrivate)
		rcText = m_rcPrivate;
	else*/
		rcText = m_rcItem;
	m_nLinks = lengthof(m_rcLinks);
	CBlueRenderEngineUI::DoPaintPrettyText(hDC, m_pManager, rcText, m_sText, m_TextColor, m_BackColor, m_rcLinks, m_nLinks, m_uTextStyle);
}

//void CTextPanelUI::SetRect(RECT rc)
//{
//	m_rcPrivate = rc;
//}
